<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        @open="opendialog"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem  :label="t('parts.entry.parts_info_id')" type="remoteSelect" v-model="baTable.form.extend.partsInfo.parts_name" prop="partsInfo.parts_name" :input-attr="{ onRow:parts_changge,pk: 'ba_parts_info.parts_name', field: 'parts_name', 'remote-url': '/admin/parts.Info/index' }" :placeholder="t('Please select field', { field: t('parts.entry.parts_info_id') })" />
                    <FormItem  label="配件编码" type="string" prop="partsInfo.parts_code" :input-attr="{ step: 1 ,readonly:true}" v-model.number="baTable.form.extend!.partsInfo.parts_code" />
                    <FormItem  label="配件图片" type="image" v-model="baTable.form.extend!.partsInfo.parts_img" prop="partsInfo.parts_img" :input-attr="{disabled:true }"/>
                    <FormItem  label="配件厂家" type="string" v-model="baTable.form.extend!.manufacturerInfo.manufacturer_name" prop="manufacturerInfo.manufacturer_name" :input-attr="{readonly:true }"/>
                    <FormItem :label="t('parts.entry.parts_cost')" type="number" prop="parts_cost" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.parts_cost" :placeholder="t('Please input field', { field: t('parts.entry.parts_cost') })" />
                    <FormItem :label="t('parts.entry.parts_money')" type="number" prop="parts_money" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.parts_money" :placeholder="t('Please input field', { field: t('parts.entry.parts_money') })" />
                    <FormItem :label="t('parts.entry.entry_nur')" type="number" prop="entry_nur" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.entry_nur" :placeholder="t('Please input field', { field: t('parts.entry.entry_nur') })" />
                    <FormItem :label="t('parts.entry.entry_date')" type="date" v-model="baTable.form.items!.entry_date" prop="entry_date" :placeholder="t('Please select field', { field: t('parts.entry.entry_date') })" />
                    <FormItem :label="t('parts.entry.remark')" type="textarea" v-model="baTable.form.items!.remark" prop="remark" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('parts.entry.remark') })" />
                    <template v-show="false" >
                        <FormItem  label="审核" type="string" prop="approval" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend!.approval" />
                    </template>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="onsubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
    <el-dialog v-model="from.dialogFormVisible" title="申请" width="500">
      <el-form :model="from.tables">
        <el-form-item label="说明：" :label-width="from.formLabelWidth">
          <el-input type="textarea" v-model="baTable.form.items!.approval" autocomplete="off" input-attr="{ rows: 3 }"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="from.dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="InitiateReview">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

let formdata:any
const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()
const from = reactive({
  dialogFormVisible:false,
  tables:[],
  name:'',
  formLabelWidth:80
})

const opendialog = function ()
{


}
const onsubmit = function (fdata)
{
  if(baTable.form.operate != "Add")
  {
    baTable.form.items.approval = ''
    formdata = fdata;
    from.dialogFormVisible = true;
  }else {
    baTable.onSubmit(fdata);
  }
}
const InitiateReview = function (){
  baTable.form.extend.audit_description = from.name;
  from.dialogFormVisible = false;
  baTable.onSubmit(formdata);
}
const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    parts_info_id: [buildValidatorData({ name: 'required', title: t('parts.entry.parts_info_id') })],
    parts_cost: [buildValidatorData({ name: 'number', title: t('parts.entry.parts_cost') }), buildValidatorData({ name: 'required', title: t('parts.entry.parts_cost') })],
    parts_money: [buildValidatorData({ name: 'number', title: t('parts.entry.parts_money') })],
    entry_nur: [buildValidatorData({ name: 'number', title: t('parts.entry.entry_nur') }), buildValidatorData({ name: 'required', title: t('parts.entry.entry_nur') })],
    entry_date: [buildValidatorData({ name: 'date', title: t('parts.entry.entry_date') }), buildValidatorData({ name: 'required', title: t('parts.entry.entry_date') })],
    create_time: [buildValidatorData({ name: 'date', title: t('parts.entry.create_time') })],
})
const parts_changge = function (data){
  baTable.form.extend.partsInfo.parts_code = data.parts_code;
  baTable.form.extend.partsInfo.parts_img = data.parts_img;
  baTable.form.extend.manufacturerInfo.manufacturer_info_id = data.manufacturer_info_id;
  baTable.form.extend.manufacturerInfo.manufacturer_name = data.manufacturerInfo.manufacturer_name;
  baTable.form.items.parts_info_id = data.id;
}
</script>

<style scoped lang="scss"></style>
